package com.arqui.admmon_module.client.core.adm_brand;

import com.gwtplatform.dispatch.shared.DispatchAsync;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.arqui.admmon_module.client.place.NameTokens;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.google.inject.Inject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LongBox;
import com.google.gwt.user.client.ui.TextBox;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.arqui.admmon_module.client.action.SaveBrand;
import com.arqui.admmon_module.client.action.SaveBrandResult;
import com.arqui.admmon_module.client.core.main.HeaderPresenter;

public class BrandModPresenter extends
		Presenter<BrandModPresenter.MyView, BrandModPresenter.MyProxy> {
	
	@Inject	PlaceManager placeManager;
	@Inject	DispatchAsync dispatcher;

	public interface MyView extends View {
		
		public TextBox getLabelName();

		public TextBox getLabelNameRepre();

		public TextBox getLabelLastNameRepre();

		public LongBox getLongIdRepre();

		public TextBox getLabelEMail();

		public LongBox getLongCel();
		
		public LongBox getLongNit();
		
		public Button getButtonSave();

		public Button getButtonCancel();
		
		public Label getMessage();
	}

	@ProxyCodeSplit
	@NameToken(NameTokens.brandmod)
	public interface MyProxy extends ProxyPlace<BrandModPresenter> {
	}

	@Inject
	public BrandModPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy) {
		super(eventBus, view, proxy);
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, HeaderPresenter.SLOT_content, this);
	}

	@Override
	protected void onBind() {
		super.onBind();
	}

	@Override
	protected void onReset() {
		super.onReset();
		
		getView().getButtonSave().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				SaveBrand action = new SaveBrand(getView().getLabelName().getText(), getView().getLongNit().getValue(), getView().getLabelNameRepre().getText(), getView().getLabelLastNameRepre().getText(),
						getView().getLongIdRepre().getValue(), getView().getLabelEMail().getText(), getView().getLongCel().getValue());
				dispatcher.execute(action,saveCallBack);
			}
		});
	}
	
	private AsyncCallback<SaveBrandResult> saveCallBack = new AsyncCallback<SaveBrandResult>() {
		@Override
		public void onSuccess(SaveBrandResult result) {
			if(result.getState()){
				PlaceRequest request = new PlaceRequest(NameTokens.main);
				placeManager.revealPlace(request);
			}else{
				getView().getMessage().setText(result.getMessage());
			}
		}
		
		@Override
		public void onFailure(Throwable caught) {
			getView().getMessage().setText("Lo sentimos, Ocurrio un error inesperado");
			}
		
	};
}
